import axios from "axios";

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    
    return response;
  }, function (error) {
    // 对响应错误做点什么
    
    return Promise.reject(error);
  });

class Http_axios{
    constructor(){
        
    }

    request( rdata ){
        let m = rdata.method;
        let lm = m.toLowerCase();
        return  new Promise( function( resolve , reject ){
          axios.request({
              url: rdata.url,
              baseURL: rdata.baseurl,
              method: m,
              data: lm != "get" ?  ( rdata.data ? rdata.data : null ) : null,
              params: lm == "get" ? rdata.data : null, 
              headers: rdata.header ? rdata.header : null,
              withCredentials: true
          }).then( res =>{
            resolve( res )
          }).catch(function (error) {
            if (error.response) {
              // The request was made and the server responded with a status code
              // that falls out of the range of 2xx
              // console.log(error.response.data);
              // console.log(error.response.status);
              // console.log(error.response.headers);
              reject(error.response);
            } else if (error.request) {
              // The request was made but no response was received
              // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
              // http.ClientRequest in node.js
              // console.log(error.request);
              reject(error.request);
            } else {
              // Something happened in setting up the request that triggered an Error
              console.log('Error', error.message);
              reject(error.message);
            }
            // console.log(error.config);
          });
        } ); 
        
        
            
        
    }

    deliverObj(){

    }
}

export default new Http_axios();